package com.ruoyi.web.controller.loss;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.loss.domain.bo.WellDetailDataBo;
import com.ruoyi.loss.domain.vo.WellDetailDataVo;
import com.ruoyi.loss.listener.WellDetailDataImportListener;
import com.ruoyi.loss.service.IWellDetailDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 钻井-详细信息
 *
 * @author yangyong
 * @date 2023-12-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/loss/wellDetailData")
public class WellDetailDataController extends BaseController {

    private final IWellDetailDataService iWellDetailDataService;

    /**
     * 查询钻井-详细信息列表
     */
    @SaCheckPermission("helper:wellDetailData:list")
    @GetMapping("/list")
    public TableDataInfo<WellDetailDataVo> list(WellDetailDataBo bo, PageQuery pageQuery) {
        return iWellDetailDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出钻井-详细信息列表
     */
    @SaCheckPermission("helper:wellDetailData:export")
    @Log(title = "钻井-详细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WellDetailDataBo bo, HttpServletResponse response) {
        List<WellDetailDataVo> list = iWellDetailDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "钻井-详细信息", WellDetailDataVo.class, response);
    }

    /**
     * 获取钻井-详细信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("helper:wellDetailData:query")
    @GetMapping("/{id}")
    public R<WellDetailDataVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iWellDetailDataService.queryById(id));
    }

    /**
     * 新增钻井-详细信息
     */
    @SaCheckPermission("helper:wellDetailData:add")
    @Log(title = "钻井-详细信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WellDetailDataBo bo) {
        return toAjax(iWellDetailDataService.insertByBo(bo));
    }

    /**
     * 修改钻井-详细信息
     */
    @SaCheckPermission("helper:wellDetailData:edit")
    @Log(title = "钻井-详细信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WellDetailDataBo bo) {
        return toAjax(iWellDetailDataService.updateByBo(bo));
    }

    /**
     * 删除钻井-详细信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("helper:wellDetailData:remove")
    @Log(title = "钻井-详细信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWellDetailDataService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "钻井数据", businessType = BusinessType.IMPORT)
    @SaCheckPermission("helper:wellDetailData:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<WellDetailDataVo> result = ExcelUtil.importExcel(file.getInputStream(), WellDetailDataVo.class, new WellDetailDataImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "钻井数据", WellDetailDataVo.class, response);
    }
}
